package util;

import android.annotation.SuppressLint;
import android.widget.TextView;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@SuppressLint({"DefaultLocale", "SimpleDateFormat"})
public class StringUtils {

    /**
     * 把输入流转换成字符数组
     *
     * @param inputStream 输入流
     * @return 字符数组
     * @throws Exception
     */
    public static byte[] readStream(InputStream inputStream) throws Exception {
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = inputStream.read(buffer)) != -1) {
            bout.write(buffer, 0, len);
        }
        bout.close();
        inputStream.close();

        return bout.toByteArray();
    }

    public static String replaceUrlWithPlus(String url) {
        if (url != null) {
            // return url.replaceAll("http://(.)*?/",
            // "").replaceAll("[.:/,%?&=]", "+").replaceAll("[+]+", "+");
            return url.replaceAll("[/|&|?|:|%]+", "_");
        }
        return null;
    }

    // 获得汉语拼音首字母
    @SuppressLint("DefaultLocale")
    public static String getAlpha(String str) {
        if (str == null) {
            return "#";
        }

        if (str.trim().length() == 0) {
            return "#";
        }

        char c = str.trim().substring(0, 1).charAt(0);
        // 正则表达式，判断首字母是否是英文字母
        Pattern pattern = Pattern.compile("^[A-Za-z]+$");
        if (pattern.matcher(c + "").matches()) {
            return (c + "").toUpperCase();
        } else {
            return "#";
        }
    }

    /**
     * 判断给定字符串是否空白串。 空白串是指由空格、制表符、回车符、换行符组成的字符串 若输入字符串为null或空字符串，返回true
     *
     * @param input
     * @return boolean
     */
    public static boolean isEmpty(String input) {
        if (input == null || "".equals(input))
            return true;

        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            if (c != ' ' && c != '\t' && c != '\r' && c != '\n') {
                return false;
            }
        }
        return true;
    }

    /*
     * 规格化时间
     */
    public static String calTime(long endTime) {
        long between = endTime - System.currentTimeMillis();
        int tmp = (int) (between / (1000 * 60 * 60 * 24));
        between = between % (1000 * 60 * 60 * 24);
        String timeStr = "";
        if (tmp > 0) {
            timeStr += String.valueOf(tmp) + "天";
        }

        tmp = (int) (between / (1000 * 60 * 60));
        between = between % (1000 * 60 * 60);
        if (tmp > 0) {
            timeStr += String.valueOf(tmp) + "小时";
        }

        tmp = (int) (between / (1000 * 60));
        between = between % (1000 * 60);
        if (tmp > 0) {
            timeStr += String.valueOf(tmp) + "分";
        }

        tmp = (int) (between / (1000));
        if (tmp > 0) {
            timeStr += String.valueOf(tmp) + "秒";
        }
        return timeStr;
    }

    public static String getCurrentTime() {
        SimpleDateFormat dateformat = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss");
        dateformat.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        Date date = new Date(System.currentTimeMillis());
        return "最近更新：" + dateformat.format(date);
    }
    public static String gettime(Long time) {
        SimpleDateFormat dateformat = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss");
        dateformat.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        return  dateformat.format(time);
    }
    /**
     * 中判断字符串是否为数字的方法
     *
     * @param str
     * @return
     */

    public static boolean isNumeric(String str) {
        // Pattern pattern = Pattern.compile("[0-9]+");
        Pattern pattern = Pattern.compile("-?[0-9]+.?[0-9]+");// 匹配所有的数字
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }

    public static boolean isLetter(char c) {
        int k = 0x80;
        return c / k == 0 ? true : false;
    }

    /**
     * 得到一个字符串的长度,显示的长度,一个汉字或日韩文长度为2,英文字符长度为1
     *
     * @param  s 需要得到长度的字符串
     * @return int 得到的字符串长度
     */
    public static int length(String s) {
        if (s == null)
            return 0;
        char[] c = s.toCharArray();
        int len = 0;
        for (int i = 0; i < c.length; i++) {
            len++;
            if (!isLetter(c[i])) {
                len++;
            }
        }
        return len;
    }

    /**
     * 得到一个字符串的长度,显示的长度,一个汉字或日韩文长度为1,英文字符长度为0.5
     *
     * @param  s 需要得到长度的字符串
     * @return int 得到的字符串长度
     */
    public static double getLength(String s) {
        double valueLength = 0;
        String chinese = "[\u4e00-\u9fa5]";
        // 获取字段值的长度，如果含中文字符，则每个中文字符长度为2，否则为1
        for (int i = 0; i < s.length(); i++) {
            // 获取一个字符
            String temp = s.substring(i, i + 1);
            // 判断是否为中文字符
            if (temp.matches(chinese)) {
                // 中文字符长度为1
                valueLength += 1;
            } else {
                // 其他字符长度为0.5
                valueLength += 0.5;
            }
        }
        // 进位取整
        return Math.ceil(valueLength);
    }

    public static void filtNull(TextView textView, String s) {
        if (s != null) {
            textView.setText(s);
        } else {
            textView.setText(filtNull(s));
        }
    }

    // 判断过滤单个string为null
    public static String filtNull(String s) {
        if (s != null) {
            return s;
        } else {
            s = "";
        }
        return s;
    }

    public static String replacePhone(String Phone) {
        String str = "";
        StringBuffer buffer = new StringBuffer(Phone);
        str = String.valueOf(buffer.replace(3, 7, "****"));
        return str;
    }

    /**
     * 效验小数点后面两位
     *
     * @param str
     * @return
     */
    public static boolean isNumber(String str) {
        Pattern pattern = Pattern
                .compile("^(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){0,2})?$"); // 判断小数点后2位的数字的正则表达式
        Matcher match = pattern.matcher(str);
        if (match.matches() == false) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 判断服务器返回数据是否为空
     *
     * @param info
     * @return
     */
    public static String retrunInfo(String info) {
        if (isEmpty(info)) {
            return "";
        } else {
            return info;
        }
    }

}
